package com.fates.ucenter.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fates.ucenter.constant.ApiConstant;
import com.fates.ucenter.entity.po.User;
import com.fates.ucenter.result.BaseResponseInfo;
import com.fates.ucenter.result.ResultErrorCode;
import com.fates.ucenter.service.UserService;
import com.fates.ucenter.common.BaseController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @since 2017-09-07
 */
@Controller
@RequestMapping(ApiConstant.RMS_USER)
public class UserController extends BaseController {

    @Autowired
    private UserService userService;


    /**
     * 添加用户
     */
    @ResponseBody
    @RequestMapping(value = "",method={ RequestMethod.POST })
    public BaseResponseInfo addUser(@Valid User user, BindingResult bindingResult) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
        if (bindingResult.hasErrors()) {
        	info.code=ResultErrorCode.InternalError.getCode();
        	return info;
        }
        userService.addOrUpdateUser(user);
        return info;
    }

    /**
     * 获取用户列表
     */
    @ResponseBody
    @RequestMapping(value = "",method={ RequestMethod.GET })
    public BaseResponseInfo findAllUser(
            @RequestParam(value = "showCount", required = false, defaultValue = "10") Integer showCount,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	List<User> list =userService.selectList(new EntityWrapper());
    	info.data=list;
        return info;
    }

   
    /**
     * 查询单个用户
     */
    @ResponseBody
    @RequestMapping(value = "/{id}",method={ RequestMethod.GET })
    public BaseResponseInfo findUserById(@PathVariable("id") Integer id) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	//List<User> list =userService.selectList(new EntityWrapper().where("id = {0}", id));
    	info.data=userService.selectUserById(id);
        return info;
    }

    /**
     * 查询单个用户
     */
    @ResponseBody
    @RequestMapping(value = "/account",method={ RequestMethod.POST })
    public BaseResponseInfo findUserByAccout(@RequestParam("account") String account) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
    	//List<User> list =userService.selectList(new EntityWrapper().where("account = {0}", account));
    	info.data=userService.selectUserByAccout(account);
        return info;
    }

    /**
     * 删除用户
     */
    @ResponseBody
    @RequestMapping(value = "/{id}",method={ RequestMethod.DELETE })
    public BaseResponseInfo deleteUserById(@PathVariable("id") Integer id) throws Exception {
    	BaseResponseInfo info = new BaseResponseInfo();
        if (!userService.deleteById(id)) {
        	 info.code=ResultErrorCode.InternalError.getCode();
        }
       
        return info;
    }
//
//    @RequestMapping(value = "/test", method = {RequestMethod.GET, RequestMethod.POST})
//    public String redisTest(){
//        StringBuffer sb = new StringBuffer();
//        redisService.set("str", "str");
//        sb.append("str=").append(redisService.get("str").toString()).append(",");
//        redisService.hmSet("hmset","key","val");
//        sb.append("hmset=").append(redisService.hmGet("hmset","key")).append(",");
//        redisService.lPush("list","val");
//        sb.append("list=").append(redisService.lRange("list",0,1).toString()).append(",");
//        redisService.add("set","val");
//        sb.append("set=").append(redisService.setMembers("set").toString()).append(",");
//        redisService.zAdd("zset","val1",1);
//        redisService.zAdd("zset","val2",2);
//        sb.append("zset=").append(redisService.rangeByScore("zset",1,2)).append(",");
//        return sb.toString();
//    }
}
